home *** CD-ROM | disk | FTP | other *** search
/ PCGUIA 117 / PC Guia 117.iso / Software / Utils / Software2 / Product11 / Setup.exe / MT-3.16-full-en_US / mt-db2sql.cgi < prev    next >
Text File  |  2005-03-08  |  5KB  |  148 lines

  1. #!/usr/bin/perl -w
  2. # Copyright 2001-2005 Six Apart. This code cannot be redistributed without
  3. # permission from www.movabletype.org.
  4. #
  5. # $Id: mt-db2sql.cgi 10197 2005-03-09 00:27:57Z ezra $
  6. use strict;
  7.  
  8. my($MT_DIR);
  9. BEGIN {
  10.     if ($0 =~ m!(.*[/\\])!) {
  11.         $MT_DIR = $1;
  12.     } else {
  13.         $MT_DIR = './';
  14.     }
  15.     unshift @INC, $MT_DIR . 'lib';
  16.     unshift @INC, $MT_DIR . 'extlib';
  17. }
  18.  
  19. local $| = 1;
  20. print "Content-Type: text/html\n\n";
  21. print "<pre>\n\n";
  22.  
  23. my @CLASSES = qw( MT::Author MT::Blog MT::Category MT::Comment MT::Entry
  24.                   MT::IPBanList MT::Log MT::Notification MT::Permission
  25.                   MT::Placement MT::Template MT::TemplateMap MT::Trackback
  26.                   MT::TBPing );
  27.  
  28. use File::Spec;
  29.  
  30. eval {
  31.     local $SIG{__WARN__} = sub { print "**** WARNING: $_[0]\n" };
  32.  
  33.     require MT;
  34.     my $mt = MT->new( Config => $MT_DIR . 'mt.cfg', Directory => $MT_DIR )
  35.         or die MT->errstr;
  36.  
  37.     require MT::Object;
  38.     my($type) = $mt->{cfg}->ObjectDriver =~ /^DBI::(.*)$/;
  39.     MT::Object->set_driver('DBI::' . $type)
  40.         or die MT::ObjectDriver->errstr;
  41.     my $dbh = MT::Object->driver->{dbh};
  42.     my $schema = File::Spec->catfile($MT_DIR, 'schemas', $type . '.dump');
  43.     open FH, $schema or die "Can't open schema file '$schema': $!";
  44.     my $ddl;
  45.     { local $/; $ddl = <FH> }
  46.     close FH;
  47.     my @stmts = split /;/, $ddl;
  48.     print "Loading database schema...\n\n";
  49.     for my $stmt (@stmts) {
  50.         $stmt =~ s!^\s*!!;
  51.         $stmt =~ s!\s*$!!;
  52.         next unless $stmt =~ /\S/;
  53.         $dbh->do($stmt) or die $dbh->errstr;
  54.     }
  55.  
  56.     ## %ids will hold the highest IDs of each class.
  57.     my %ids;
  58.  
  59.     print "Loading data...\n";
  60.     for my $class (@CLASSES) {
  61.         print $class, "\n";
  62.         MT::Object->set_driver('DBM');
  63.         eval "use $class";
  64.         my $iter = $class->load_iter;
  65.  
  66.         my %names;
  67.  
  68.         MT::Object->set_driver('DBI::' . $type);
  69.         while (my $obj = $iter->()) {
  70.             print "    ", $obj->id, "\n";
  71.             $ids{$class} = $obj->id
  72.                 if !$ids{$class} || $obj->id > $ids{$class};
  73.             ## Look for duplicate template, category, and author names,
  74.             ## because we have uniqueness constraints in the DB.
  75.             if ($class eq 'MT::Template') {
  76.                 my $key = lc($obj->name) . $obj->blog_id;
  77.                 if ($names{$class}{$key}++) {
  78.                     print "        Found duplicate template name '" .
  79.                           $obj->name;
  80.                     $obj->name($obj->name . ' ' . $names{$class}{$key});
  81.                     print "'; renaming to '" . $obj->name . "'\n";
  82.                 }
  83.                 ## Touch the text column to make sure we read in
  84.                 ## any linked templates.
  85.                 my $text = $obj->text;
  86.             } elsif ($class eq 'MT::Author') {
  87.                 my $key = lc($obj->name);
  88.                 if ($names{$class . $obj->type}{$key}++) {
  89.                     print "        Found duplicate author name '" .
  90.                           $obj->name;
  91.                     $obj->name($obj->name . ' ' . $names{$class}{$key});
  92.                     print "'; renaming to '" . $obj->name . "'\n";
  93.                 }
  94.                 $obj->email('') unless defined $obj->email;
  95.                 $obj->set_password('') unless defined $obj->password;
  96.             } elsif ($class eq 'MT::Category') {
  97.                 my $key = lc($obj->label) . $obj->blog_id;
  98.                 if ($names{$class}{$key}++) {
  99.                     print "        Found duplicate category label '" .
  100.                           $obj->label;
  101.                     $obj->label($obj->label . ' ' . $names{$class}{$key});
  102.                     print "'; renaming to '" . $obj->label . "'\n";
  103.                 }
  104.             } elsif ($class eq 'MT::Trackback') {
  105.                 $obj->entry_id(0) unless defined $obj->entry_id;
  106.                 $obj->category_id(0) unless defined $obj->category_id;
  107.             } elsif ($class eq 'MT::Entry') {
  108.                 $obj->allow_pings(0)
  109.                     if defined $obj->allow_pings && $obj->allow_pings eq '';
  110.                 $obj->allow_comments(0)
  111.                     if defined $obj->allow_comments && $obj->allow_comments eq '';
  112.             }
  113.             $obj->save
  114.                 or die $obj->errstr;
  115.         }
  116.         print "\n";
  117.     }
  118.  
  119.     if ($type eq 'postgres') {
  120.         print "Updating sequences\n";
  121.         my $dbh = MT::Object->driver->{dbh};
  122.         for my $class (keys %ids) {
  123.             print "    $class => $ids{$class}\n";
  124.             my $seq = 'mt_' . $class->datasource . '_' .
  125.                       $class->properties->{primary_key};
  126.             $dbh->do("select setval('$seq', $ids{$class})")
  127.                 or die $dbh->errstr;
  128.         }
  129.     }
  130. };
  131. if ($@) {
  132.     print <<HTML;
  133.  
  134. An error occurred while loading data:
  135.  
  136. $@
  137.  
  138. HTML
  139. } else {
  140.     print <<HTML;
  141.  
  142. Done copying data from Berkeley DB to your SQL database! All went well.
  143.  
  144. HTML
  145. }
  146.  
  147. print "</pre>\n";
  148.